package com.gtw.modules.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreSys;
import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.common.validator.ValidatorUtils;
import com.gtw.common.validator.group.AddGroup;
import com.gtw.common.validator.group.UpdateGroup;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.sys.service.SysTbUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/** 会员、代理用户
 * Created by wuye on 2018/1/26.
 */
@RestController
@RequestMapping("/sys/tbuser")
public class SysTbUserController {

    @Autowired
    private SysTbUserService sysTbUserService;

    /**
     * 所有用户列表
     */
    @AuthIgnoreSys
    @RequestMapping("/list")
    @RequiresPermissions("sys:tbuser:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = sysTbUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }


    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:tbuser:info")
    public R info(@PathVariable("userId") int userId){
        TbUser tbUser = sysTbUserService.queryObject(userId);
        return R.ok().put("tbUser", tbUser);
    }


    /**
     * 查看团队
      * @param userId
     * @return
     */
    @RequestMapping("/team/{userId}")
    @RequiresPermissions("sys:tbuser:team")
    public R findTeam(@PathVariable("userId") int userId){
        List<TbUser> list = sysTbUserService.queryTeam(userId);
        return R.ok().put("list",list);
    }

    /**
     * 查看一级团队
     * @param params
     * @return
     */
    @RequestMapping("/team1")
    public R findTeam1(@RequestParam Map<String, Object> params){
        //查询列表数据
        params.put("proxyid",params.get("proxyid"));
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = sysTbUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }

    /**
     * 查看二级团队
     * @param params
     * @return
     */
    @RequestMapping("/team2")
    public R findTeam2(@RequestParam Map<String, Object> params){
        //查询列表数据
        params.put("proxyid2",params.get("proxyid"));
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = sysTbUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }

    /**
     * 查看三级团队
     * @param params
     * @return
     */
    @RequestMapping("/team3")
    public R findTeam3(@RequestParam Map<String, Object> params){
        //查询列表数据
        params.put("proxyid3",params.get("proxyid"));
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = sysTbUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }

    /**
     * 查看四级团队
     * @param params
     * @return
     */
    @RequestMapping("/team4")
    public R findTeam4(@RequestParam Map<String, Object> params){
        //查询列表数据
        params.put("proxyid4",params.get("proxyid"));
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = sysTbUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("sys:tbuser:save")
    public R save(@RequestBody TbUser tbUser){
        ValidatorUtils.validateEntity(tbUser, AddGroup.class);

        sysTbUserService.save(tbUser);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("sys:tbuser:update")
    public R update(@RequestBody TbUser tbUser){
        ValidatorUtils.validateEntity(tbUser, UpdateGroup.class);
        sysTbUserService.update(tbUser);
        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:tbuser:delete")
    public R delete(@RequestBody int userIds){
        sysTbUserService.deleteBatch(userIds);
        return R.ok();
    }
}
